package cn.itcast.interceptor;

import cn.itcast.mapper.userMapper;
import cn.itcast.pojo.user;
import cn.itcast.pojo.userExample;
import cn.itcast.util.OutputStringController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
public class MyInterceptor implements HandlerInterceptor {
//    private static final Logger log = LoggerFactory.getLogger(MyInterceptor.class);
    @Resource
cn.itcast.mapper.userMapper userMapper;
    @Resource
    OutputStringController osc;
    //用户唯一Id
    //private  static  String openId = "";
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("会话验证拦截器开始");
        Cookie[] cookies = request.getCookies();
        //会话密匙skey
        String skey = "";

        String results = "";
        if(StringUtils.isNotBlank(ObjectUtils.toString(cookies))){
            for (Cookie cookie : cookies) {
                switch(cookie.getName()){
                    case "token":
                        skey = cookie.getValue();
                        break;
                    default:
                        break;
                }
            }
        }
        log.debug("skey="+skey);
        if(skey!=null&&!skey.equals("")&&!StringUtils.isNotBlank(ObjectUtils.toString(skey))){
            userExample userExample = new userExample();
            cn.itcast.pojo.userExample.Criteria criteria = userExample.createCriteria();
            criteria.andSkeyEqualTo(skey);
            List<user> users = userMapper.selectByExample(userExample);
            if(!users.isEmpty()){
                //openId = users.get(0).getOpenId();
                log.debug("用户openId为：");
                return true;
            }else {
                log.debug("认证失败,会话已过期重新登录");
                response.setCharacterEncoding("utf-8");
                response.getWriter().write("认证失败,会话已过期重新登录");
                return false;
            }
        }else {
            //results = osc.resultFailure("认证失败","");
            log.debug("认证失败");
            response.setCharacterEncoding("utf-8");
            response.getWriter().write("认证失败");
            return false;
        }

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.debug("postHandle method is running");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.debug("afterCompletion method is running");
    }
}
